package com.tomek.ktoryoperator.services;

import java.util.ArrayList;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;

import com.tomek.ktoryoperator.providers.ProvidersFactory;
import com.tomek.ktoryoperator.tasks.GetInfosForNumbersTask;
import com.tomek.ktoryoperator.utils.ContentProvidersUtils;
import com.tomek.ktoryoperator.utils.LogUtils;
import com.tomek.ktoryoperator.utils.PreferenceUtils;
import com.tomek.ktoryoperator.utils.NetworkUtils;

public class UpdateService extends Service {
	SharedPreferences prefs;

	@Override
	public void onCreate() {
		LogUtils.d(getClass(), "onCreate");
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);

		// FlurryAgent.onStartSession(this, C.FLURRY_KEY);
		LogUtils.d(getClass(), "onStart", "startId=%d", startId);

		if (PreferenceUtils.shouldRunInBackground(this) && NetworkUtils.isNetworkAvailable(this)
				&& NetworkUtils.backgroundDataAllowed(this)) {

			ArrayList<String> numbersToRead = ContentProvidersUtils.getNumbersToRead(getContentResolver(),
					PreferenceUtils.getNumberToReadCount(this));

			GetInfosForNumbersTask task = new GetInfosForNumbersTask(this, ProvidersFactory.getInstance()
					.getInfoProviders());
			task.execute(numbersToRead.toArray(new String[] {}));
		} else {
			LogUtils.d(getClass(), "onStart", "not allowed to start, startId=%d", startId);
		}

		LogUtils.d(getClass(), "onStart", "stopping, startId=%d", startId);

		stopSelf(startId);
	}

	@Override
	public void onDestroy() {
		LogUtils.d(getClass(), "onDestroy");
		// FlurryAgent.onEndSession(this);

		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
}